package alo.p5.work;

import java.util.Queue;
import java.util.Stack;
//逆置队列前k个元素
public class ReverseQueueFirstKElements {
    public void reverseQueueFirstKElements(Queue<Integer> queue, int k){
        if (queue==null||k>queue.size()) throw new IllegalArgumentException();
        else if (k>0){
            Stack<Integer> stack=new Stack<>();
            for (int i = 0; i < k; i++) {
                stack.push(queue.poll());
            }
            while (!stack.isEmpty()){
                queue.offer(stack.pop());
            }
            for (int i = 0; i < queue.size() - k; i++) {
                queue.offer(queue.poll());
            }
        }
    }
}
